package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.LoginActivity;
import com.payment.ktb.activity.ProtocolActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.DeviceUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.ToastUtils;
import com.payment.ktb.utils.UpdateAPKUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/4.
 */
public class SettingActivty extends BaseActivity {




    @BindView(R.id.tv_setting_version)
    TextView tv_setting_version;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initTitle("设置");
        tv_setting_version.setText(DeviceUtils.getVersionName());

    }


    @OnClick({R.id.ll_setting_loginpassword,R.id.ll_setting_procotol,R.id.ll_setting_aboutus,R.id.ll_setting_feedback,R.id.ll_setting_version,R.id.rl_setting_exit})
    public void clickEvent(View view) {
        switch (view.getId()) {

            //修改登录密码
            case R.id.ll_setting_loginpassword:
                startActivity(ModifyPasswordActivity.class);
                break;

            //用户协议
            case R.id.ll_setting_procotol:
                startActivity(ProtocolActivity.class);
                break;

            //关于我们
            case R.id.ll_setting_aboutus:
                startActivity(AboutUsActivity.class);
                break;

            //退出
            case R.id.rl_setting_exit:

                SharedPreferencesUtils.putString(ConstantsUser.userKey,"");
                SharedPreferencesUtils.putString(ConstantsUser.userToken,"");

                Intent intent = new Intent(mContext,LoginActivity.class);
                intent.putExtra("fromActivity","SettingActivity");
                startActivity(intent);
                overridePendingTransition(R.anim.activity_open,0);
                break;


            //意见反馈
            case R.id.ll_setting_feedback:
                startActivity(FeedbackActivity.class);
                break;


        }
    }





    /**
     *检测升级
     */
    public void updateCheck() {



        progressDialogUtils.showProgress();


        //构造传输参数
        String os = "ANDROID";
        String version = String.valueOf(DeviceUtils.getVersionCode());
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        //MD5
        String sign = Md5Utils.md5Hex(os + version + SharedPreferencesUtils.getString(ConstantsUser.userKey));
        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();
        map.put("os",os);
        map.put("version",version);
        map.put("token",token);
        map.put("sign",sign);


        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.shopSdkVersionState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {
                        if ("0".equals(jsonObject.optString("isUpd"))) {
                            ToastUtils.showToast("已是最新版本！");
                        } else if ("1".equals(jsonObject.optString("isUpd"))){
                            UpdateAPKUtils.updateCheck(mContext);
                        }

                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });



    }


}
